var testMenu = [
    {'id':1,'name':'aaa','appList':[
        {'id':'1_1','name':'aaa_1','able':true},{
            'id':'1_2','name':'aaa_2' ,'able':true
        },{
            'id':'1_3','name':'aaa_3','able':true
        },{
            'id':'1_4','name':'aaa_4','able':true
        }
    ],'able':true},
    {'id':2,'name':'bbb','appList':[{
        'id':'2_1','name':'bbb_1','able':true} ,
        {'id':'2_2','name':'bbb_2','able':true} ,
        {'id':'2_3','name':'bbb_3','able':true} ,
        {'id':'2_4','name':'bbb_4','able':true} 
    ],'able':true},
    {'id':3,'name':'ccc','appList':[{
        'id':'3_1','name':'ccc_1','able':true} ,
        {'id':'3_2','name':'ccc_2','able':true} ,
        {'id':'3_3','name':'ccc_3','able':true} ,
        {'id':'3_4','name':'ccc_4','able':true} 
    ],'able':true},
    {'id':4,'name':'ccc2','appList':[{
        'id':'3_1','name':'ccc_1','able':true} ,
        {'id':'3_2','name':'ccc_2','able':true} ,
        {'id':'3_3','name':'ccc_3','able':true} ,
        {'id':'3_4','name':'ccc_4','able':true} 
    ],'able':true},
    {'id':5,'name':'ccc3','appList':[{
        'id':'3_1','name':'ccc_1','able':true} ,
        {'id':'3_2','name':'ccc_2','able':true} ,
        {'id':'3_3','name':'ccc_3','able':true} ,
        {'id':'3_4','name':'ccc_4','able':true} 
    ],'able':true},
    {'id':6,'name':'ccc4','appList':[{
        'id':'3_1','name':'ccc_1','able':true} ,
        {'id':'3_2','name':'ccc_2','able':true} ,
        {'id':'3_3','name':'ccc_3','able':true} ,
        {'id':'3_4','name':'ccc_4','able':true} 
    ],'able':true}
]


var tttObj = {
	"flag": true,
	"data": [{
		"busCode": "GG",
		"busNameCn": "公共APP",
		"busNameEn": "CommonApp",
		"appList": [
        //     {
		// 	"appId": "C99GG002",
		// 	"appNameCn": "输入个人密码",
		// 	"appNameEn": "InputPersonalIdentificationNumber",
		// 	"appVersionList": ["V1.1"],
		// 	"appStatus": "S4",
		// 	"statusName": "失效"
        // }, 
        {
			"appId": "C99GG005",
			"appNameCn": "凭条打印",
			"appNameEn": "ReceiptPrint",
			"appVersionList": ["V2.2", "V2.4", "V2.0", "V2.1", "V2.3", "V2.5"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "C99GG002",
			"appNameCn": "输入个人密码",
			"appNameEn": "InputPersonalIdentificationNumber",
			"appVersionList": ["V2.2", "V2.0", "V1.0", "V2.1"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "C99GG001",
			"appNameCn": "读取介质信息",
			"appNameEn": "ReadMedium",
			"appVersionList": ["V2.2", "V2.0", "V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "C17GG001",
			"appNameCn": "读取介质信息",
			"appNameEn": "ReadMedium",
			"appVersionList": ["V0.1"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "C99GG003",
			"appNameCn": "退介质",
			"appNameEn": "ExitMedium",
			"appVersionList": ["V1.0", "V2.0", "V2.1"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "C99GG004",
			"appNameCn": "读介质并输入密码",
			"appNameEn": "ReadMediumAndInputPIN",
			"appVersionList": ["V1.0", "V2.0", "V2.1"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "QB",
		"busNameCn": "查询业务",
		"busNameEn": "QueryBus",
		"appList": [{
			"appId": "C99QB001",
			"appNameCn": "余额查询",
			"appNameEn": "QUERYBALANCE",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "PB",
		"busNameCn": "缴费业务",
		"busNameEn": "PaymentBus",
		"appList": [{
			"appId": "B99PB001",
			"appNameCn": "生活缴费",
			"appNameEn": "epay",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "MB",
		"busNameCn": "维护管理",
		"busNameEn": "MaintainBus",
		"appList": [{
			"appId": "C99MB001",
			"appNameCn": "下载密钥",
			"appNameEn": "DownloadKey",
			"appVersionList": ["V1.0", "V2.0", "V2.1"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "LB",
		"busNameCn": "贷款业务",
		"busNameEn": "LoanBus",
		"appList": [{
			"appId": "B99LB003",
			"appNameCn": "额度查询",
			"appNameEn": "quotaquery",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "B99LB004",
			"appNameCn": "自助借款",
			"appNameEn": "selfloan",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "FX",
		"busNameCn": "外汇业务",
		"busNameEn": "ForexBus",
		"appList": [{
			"appId": "B99FX001",
			"appNameCn": "结售汇额度查询",
			"appNameEn": "foreignexchangequota",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "B99FX002",
			"appNameCn": "结售汇查询",
			"appNameEn": "queryforexchange",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "FD",
		"busNameCn": "基金业务",
		"busNameEn": "FundBus",
		"appList": [{
			"appId": "C99FD001",
			"appNameCn": "快E宝签约",
			"appNameEn": "KESIGN",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "FB",
		"busNameCn": "理财业务",
		"busNameEn": "FinanceBus",
		"appList": [{
			"appId": "B99FB003",
			"appNameCn": "银行转证券",
			"appNameEn": "banktosecurities",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "B99FB001",
			"appNameCn": "查询存管账户",
			"appNameEn": "searchcapitalbalance",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}, {
			"appId": "B99FB002",
			"appNameCn": "证券转银行",
			"appNameEn": "securitiestobank",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "CT",
		"busNameCn": "捐款业务",
		"busNameEn": "ContributBus",
		"appList": [{
			"appId": "B99CT001",
			"appNameCn": "一张纸捐款",
			"appNameEn": "donation",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}, {
		"busCode": "CB",
		"busNameCn": "银行卡业务",
		"busNameEn": "BankCardBus",
		"appList": [{
			"appId": "B99CB001",
			"appNameCn": "电子现金余额查询",
			"appNameEn": "digitalbalancequery",
			"appVersionList": ["V1.0"],
			"appStatus": "S3",
			"statusName": "已上架"
		}]
	}]
}

var appObj ={
    Menu:[],
    dbMenu:[],
    showMenu:[],
    
    size:{
    },
}
var isDrag = false;
var identDom = $('.ident')
var multipleBox = $('.morexy')
var startX = null;
var startY = null;
var startOverPlace = null;
var moveDom = null;
var checkItem = null;
var startIndex = null;
var isSelected = false;
var oldCol = null;
var oldFew = null;
var identMobieX = null;
var identMobieY = null;

var box_w = $('.main').width();
var box_h = $('.main').height();

var isClick = false;
var mouseFirstTime = 0;
var mouseLastTime = 0;
var overPlace = ''

var selectedObj ={}

var orgSelectedMap = {}
var dbSelectedMap = {}
var mainMenuMap = {}

var item_w = 140
var item_h = 140

var mItem_w = 140
var mitem_h = 140

var cItem_w = 140
var cItem_h = 140
var newOrAll = 0;

var imgIconMap ={
    'GG':'publicApp',
    "QB":'B07_00QUERY',
    "PB":'180000',
    "MB":'B16_00DQYW',
    "LB":'B15_00LOAN',
    "FX":'B08_00FOREXC',
    "FD":'B06_00FUND',
    "FB":'B05_00FINANCE',
    "CT":'一张纸捐款',
    "CB":'B01_00CARD',
}

init()
function init(){
    
    getData('bist/appVersionMgt/searchBusinessLineList.action',{}).then(json=>{
        console.log(json)

        appObj.Menu = json.data
        for(let i =0;i<appObj.Menu.length;i++){
            for(let j=0;j<appObj.Menu[i].appList.length;j++){
                appObj.Menu[i].appList[j].indexMap = i+'_'+j
                appObj.Menu[i].appList[j].able=true
            }

            appObj.Menu[i].indexMap = i
            appObj.Menu[i].able=true
            mainMenuMap[i]=appObj.Menu[i].busCode
            
        }

        addItem(appObj.Menu,'mainMenu','originMenu')
        addItem(appObj.Menu[0].appList,'childApps','originMenu')
        $('.childApps').eq(0).data({
            'id':appObj.Menu[0].busCode,
            'item':0
        })
    })
    

    mItem_w = mItem_h = parseInt($('.mItem').width()) +parseInt($('.mItem').css('marginLeft')) * 2
    cItem_w = cItem_h = parseInt($('.cItem').width()) +parseInt($('.cItem').css('marginLeft')) * 2

    appObj.size['mainMenu_w']=$('.mainMenu').width()
    appObj.size['childApps_w']=$('.childApps').width()
    appObj.size['mainMenu_col_len']=Math.floor(appObj.size['mainMenu_w'] / mItem_w)
    appObj.size['childApps_col_len']=Math.floor(appObj.size['childApps_w'] / cItem_w)

    

    

    

    appObj['mainMenuOrg']={
        width: $('.mainMenu').eq(0).width(),
        height: $('.mainMenu').eq(0).height(),
        top:$('.mainMenu').eq(0).offset().top,
        left:$('.mainMenu').eq(0).offset().left
    }
    appObj['mainMenuDb']={
        width: $('.mainMenu').eq(1).width(),
        height: $('.mainMenu').eq(1).height(),
        top:$('.mainMenu').eq(1).offset().top,
        left:$('.mainMenu').eq(1).offset().left
    }
    appObj['childAppsOrg']={
        width: $('.childApps').eq(0).width(),
        height: $('.childApps').eq(0).height(),
        top:$('.childApps').eq(0).offset().top,
        left:$('.childApps').eq(0).offset().left
    }
    appObj['childAppsDb']={
        width: $('.childApps').eq(1).width(),
        height: $('.childApps').eq(1).height(),
        top:$('.childApps').eq(1).offset().top,
        left:$('.childApps').eq(1).offset().left
    }
    appObj['dbPart']={
        width: $('.dbPart').width(),
        height: $('.dbPart').height(),
        top:$('.dbPart').offset().top,
        left:$('.dbPart').offset().left
    }
    appObj['orgPart']={
        width: $('.orgPart').width(),
        height: $('.orgPart').height(),
        top:$('.orgPart').offset().top,
        left:$('.orgPart').offset().left
    }
    multipleBox.css({
        width:mItem_w,
        height:mItem_h,
        marginTop:appObj.mainMenuOrg.top +'px',
        marginLeft: -appObj.childAppsOrg.left+'px'
    })

    


}
//'bist/appVersionMgt/searchBusinessLineList.action'
function getData(url,param){
    var p = new Promise(function(resolve,reject){
        // $.post(url,param,function(json){
        //     resolve(json)
        // }).fail(function(e){
        //     reject(e)
        // })

        setTimeout(function(){
            resolve(tttObj)
        },1000)
    })
    return p
}

$('.mainMenu').on('click','.mItem',function(){
    if(isClick){
        if($(this).html()=='')return
        let id =$(this).data('id')
        let type =  $(this).parent().data('type')
        let index = type=='dbMenu'?1:0;
        

        $('.childApps').eq(index).data({
            'id':id,
            'item': $(this).attr('item')
        })
        

        let list = type=='originMenu'?appObj.Menu.filter((res)=>{return res.busCode==id}):appObj.dbMenu.filter((res)=>{return res.busCode==id})     //appObj.Menu[index].appList:appObj.dbMenu[index].appList

        addItem(list[0].appList, 'childApps',type)

        isClick = false;
        mouseinit();
    }
})


$('.childApps').on('click','.cItem',function(){
    if(isClick){
        if($(this).html()=='')return
        let item =  $(this).parent().data('item')
        let type =  $(this).parent().data('type')
        let index = type=='dbMenu'?1:0;

        if($(this).hasClass('cur')){
            $(this).removeClass('cur')
            if(!$(this).siblings().hasClass('cur')){
                $('.mainMenu').eq(index).find('.mItem').eq(item).removeClass('cur')
            }

            if(startOverPlace=='OrgPart_childAppsOrg'){
                delete orgSelectedMap[$(this).attr('indexmap')]
            }else if(startOverPlace=='DbPart_childAppsDb'){
                delete dbSelectedMap[$(this).attr('indexmap')]
            }
        }else{
            
            if(index==0){
                let indexMap = $(this).attr('indexmap')
                $('.childApps').eq(0).find('[indexmap='+indexMap+']').removeClass('cur')
            }
            $(this).addClass('cur')
            $('.mainMenu').eq(index).find('.mItem').eq(item).addClass('cur')
            if(startOverPlace=='OrgPart_childAppsOrg'){
                orgSelectedMap[$(this).attr('indexmap')]=true
            }else if(startOverPlace=='DbPart_childAppsDb'){
                dbSelectedMap[$(this).attr('indexmap')]=true
            }
        }
        

    }
})
function checkBoxsPosition(pageX,pageY){
    //var domTop = $(document).scrollTop()  //页面滚动距离
    overPlace = '';
    

    if( appObj.mainMenuOrg.left < pageX && pageX < appObj.mainMenuOrg.left + appObj.mainMenuOrg.width && appObj.mainMenuOrg.top < pageY && pageY < appObj.mainMenuOrg.top + appObj.mainMenuOrg.height){
        overPlace ='OrgPart_mainMenuOrg'
        //return 'mainMenuOrg'
    }else if(appObj.childAppsOrg.left < pageX && pageX < appObj.childAppsOrg.left + appObj.childAppsOrg.width && appObj.childAppsOrg.top < pageY && pageY < appObj.childAppsOrg.top + appObj.childAppsOrg.height){
        overPlace = 'OrgPart_childAppsOrg'
        //return
    }
    else if(appObj.mainMenuDb.left < pageX && pageX < appObj.mainMenuDb.left + appObj.mainMenuDb.width && appObj.mainMenuDb.top < pageY && pageY < appObj.mainMenuDb.top + appObj.mainMenuDb.height){
        overPlace = 'DbPart_mainMenuDb'
        //return
    }else if(appObj.childAppsDb.left < pageX && pageX < appObj.childAppsDb.left + appObj.childAppsDb.width && appObj.childAppsDb.top < pageY && pageY < appObj.childAppsDb.top + appObj.childAppsDb.height){
        overPlace = 'DbPart_childAppsDb'
       
    }else if(appObj.orgPart.left < pageX && pageX < appObj.orgPart.left + appObj.orgPart.width && appObj.orgPart.top < pageY && pageY < appObj.orgPart.top + appObj.orgPart.height){
        overPlace = 'OrgPart'
    }else if(appObj.dbPart.left < pageX && pageX < appObj.dbPart.left + appObj.dbPart.width && appObj.dbPart.top < pageY && pageY < appObj.dbPart.top + appObj.dbPart.height){
        overPlace = 'DbPart'
    }
    
    return overPlace
}

function addItem(arr,className,type,redraw){
    

   let list = arr
   .filter((res) => {
    return res.able == true;
  });
    let index = type=='dbMenu'?1:0;
    
    let name = className.indexOf('mainMenu')>-1?'mItem':'cItem'
    let col_len = appObj.size[className+'_col_len']
    item_w = eval(name+'_w')
    item_h = item_w
    if($('.'+className).find('.'+name).length == arr.length && redraw){ 
        /**
         * 排序动画
         * 优化方向
         * 遍历 dom 和 arr 判断 dom中arr是否都存在 不存在的 append一下
         * 再进行 redraw
         * 
         */
        for(let i = 0;i<arr.length;i++){
            let item = $('.'+className+'[data-type='+type+']').find('.'+name+'[data-id='+arr[i][name=='mItem'?'busCode':'appId']+']')
            var col_aliquot = i % col_len ;
            var row_aliquot = Math.floor(i / col_len);
            var index_col = item_w * (col_aliquot);
            var index_few = item_h * row_aliquot;
            draw(item, index_col, index_few, 200);
            item.attr({
                'item':i,
                "col": index_col,
                "few": index_few
            })
            //console.log(item)
        }
    }else{
        $('.'+className).eq(index).html('');
        let numIndex=0
        for(let i in list){
            if(list[i].able){
                
                let obj = list[i]
                var col_aliquot = i % col_len ;
                var row_aliquot = Math.floor(i / col_len);
                var index_col = item_w * (col_aliquot);
                var index_few = item_h * row_aliquot;

                let indexMap = list[i].indexMap

                if(name=='mItem'){
                    $('.'+className).eq(index).append('<div class="mItem" item="'+i+'" col="'+index_col+'"  few="'+index_few+'" data-id="'+obj[name=='mItem'?'busCode':'appId']+'" indexMap="'+indexMap+'"><img class="icon" src="img/'+imgIconMap[obj.busCode]+'.png" /><img class="icon curicon" src="img/'+imgIconMap[obj.busCode]+'1.png" /><div class="title">'+obj.busNameCn+'</div></div>')
                    let dom  = $('.'+className).eq(index).find('.'+name).eq(i)
                    draw(dom, index_col, index_few, 0);
                }else{
                    let versionArr = obj.appVersionList.sort()
                    if(newOrAll=='1'){
                        let index_col,index_few;
                        for(let j=versionArr.length-1;j>=0;j--){

                            let col_aliquot = numIndex % col_len ;
                            let row_aliquot = Math.floor( numIndex / col_len);
                            index_col = item_w * (col_aliquot);
                            index_few = item_h * row_aliquot;
                            $('.'+className).eq(index).append('<div class="cItem" item="'+numIndex+'" col="'+index_col+'"  few="'+index_few+'" data-id="'+obj.appId+'" indexMap="'+indexMap+'"><img class="icon" src="./tezmls.PNG" /><div class="item_checkbox"></div><div class="version">'+versionArr[j]+'</div></div>')
                            let dom  = $('.'+className).eq(index).find('.'+name).eq(numIndex)
                            draw(dom, index_col, index_few, 0);
                            numIndex++;
                        }
                        
                    }else{
                        
                        $('.'+className).eq(index).append('<div class="cItem" item="'+i+'" col="'+index_col+'"  few="'+index_few+'" data-id="'+obj.appId+'" indexMap="'+indexMap+'"><img class="icon" src="./tezmls.PNG" /><div class="item_checkbox"></div><div class="version">'+versionArr[versionArr.length-1]+'</div></div>')
                        let dom  = $('.'+className).eq(index).find('.'+name).eq(i)
                        draw(dom, index_col, index_few, 0);
                    }
                    
                }


                
            }

        }
        numIndex++;
    }
    
    
}

function childAble(parentObj,Booleans){
    parentObj.appList.forEach((res)=>{
        res.able=Booleans
    })
}


function changeSort(toposion,startIndex,itemidarr,className,type){
    //console.log(toposion,startIndex)

    if(startIndex==toposion || startIndex+1==toposion){

    }else{
        toposion = itemidarr.length>toposion?toposion:itemidarr.length-1
        let arr = itemidarr[startIndex]
        itemidarr.splice(startIndex,1)
        itemidarr.splice(toposion,0,arr)
    }
    
    //console.log(itemidarr)
    //itemidarr[toposion] = itemidarr.splice(startIndex, 1, itemidarr[toposion])[0]
    addItem(itemidarr,className,type,true)
    //redraw(toposion)
}

function changeArrSort(toposion,selectArr,itemidarr,className,type){
    //需要删除 原有 selectArr 再执行此方法

    toposion = itemidarr.length>toposion?toposion:itemidarr.length-1
    toposion = itemidarr.length==0?0:toposion
    //itemidarr.splice(toposion,0,selectArr)

    for(let i=selectArr.length;i>0;i--){
        itemidarr.splice(toposion,0,selectArr[i-1])
    }
    addItem(itemidarr,className,type)
}

function draw(dom, col, few, slidetime) {
    dom.css({
      "transition-duration": slidetime + "ms",
      "transform": "translate(" + col + "px," + few + "px)",
    });
}



function mouseinit() {
    moveDom = null;
    isDrag = false;
    checkItem.css({
      "opacity": "1",
      "z-index": ""
    });
    multipleBox.hide();
    identDom.hide();
  }

$('.main').on({
    mousedown:function(e){
        e.preventDefault();
        mouseFirstTime = new Date().getTime();
        let $parent = $(e.target).parent()
        var isIdclass = $parent.hasClass('mItem')||$parent.hasClass('cItem');
        
        if(isIdclass){
            checkItem = $parent;
        }else{
            return
        }

        let name = $parent.attr('class').indexOf('mainMenu')>-1?'mItem':'cItem'

        item_w = eval(name+'_w')
        item_h = item_w
        //多选时用的
        selectedObj={}
        isSelected = false;
        moveDom = checkItem;
        
        
        startOverPlace = checkBoxsPosition(e.pageX,e.pageY)
        //console.log(startOverPlace)
        startX = e.pageX;
        startY = e.pageY;
        startIndex = parseInt(checkItem.attr("item"));
        oldCol = parseInt(checkItem.attr("col"));
        oldFew = parseInt(checkItem.attr("few"));
        //checkItem = getchekobj();
        checkItem.css({
          "opacity": "0.2",
          "z-index": "10"
        });

        // if(checkItem.hasClass('cur')||checkItem.hasClass('mItem')){
        //     //console.log(checkItem.parent().find('.cur'))

        //     let type = checkItem.parent().data('type')
        //     let sObj = type=='originMenu'?orgSelectedMap:dbSelectedMap
        //     if(Object.keys(sObj).length>1 ||checkItem.hasClass('mItem')){
        //         isSelected=true
        //         // draw(multipleBox, oldCol, oldFew, 0);
        //          moveDom = multipleBox;
        //     }
        //     //selectedObj=checkItem.parent().find('.cur')
        //     // if(selectedObj.length>1){
        isSelected=true
        //     //     // draw(multipleBox, oldCol, oldFew, 0);
         moveDom = multipleBox;
        //     // }
            
        // }
        identDom.css({height:item_h})
        draw(identDom, oldCol, oldFew, 0);
    },
    mousemove:function(e){
        if (moveDom == null) {
            return;
        }
        if (e.which != 1) {
            return;
        }
        checkBoxsPosition(e.pageX,e.pageY)

        //console.log(startOverPlace,overPlace)
        var gapX = e.pageX - startX;
        var gapY = e.pageY - startY;
        var mobiex = oldCol + gapX;
        var mobiey = oldFew + gapY - checkItem.parent().scrollTop();
        //console.log(oldFew,gapY)
        // if (mobiex > box_w - item_w) {
        //   mobiex = box_w - item_w;
        // } else if (mobiex < 0) {
        //   //mobiex = 0;
        // }
        // if (mobiey > box_h - item_h) {
        //   mobiey = box_h - item_h;
        // } else if (mobiey < 0) {
        //   //mobiey = 0;
        // }
        if (Math.abs(gapX) > 10 || Math.abs(gapY) > 10) {
          if (!isDrag) {
            if (isSelected) {
              

            //   for(let i=0;i<selectedObj.length;i++){
            //     selectedObj.eq(i).css({
            //         "opacity": "0.25",
            //         "z-index": "10"
            //       })
            //       draw(selectedObj.eq(i), oldCol, oldFew, 300);
            //   }
              
              
              moveDom.show();
            }
          }
          //if(isSelected){
            draw(moveDom, mobiex+appObj[startOverPlace.split('_')[1]].left+30, mobiey+30, 0);
        //   }else{
        //     draw(moveDom, mobiex, mobiey, 0);
        //   }
          
          
          if(overPlace=='OrgPart_mainMenuOrg'){
              //debugger;
          }
          if(overPlace=='DbPart_mainMenuDb'){
            //debugger;
        }
          //console.log(overPlace)
          if(overPlace.indexOf('Db')>-1){
            let target = $(e.target)
            let className = target.hasClass('mItem')?'mItem':'cItem'
            if(isSelected){
                switch (startOverPlace){
                    case 'OrgPart_mainMenuOrg':
                        className = 'mItem'
                        //col_len = appObj.size.mainMenuOrg_col_len
                        break;
                    case 'OrgPart_childAppsOrg':
                        className = 'cItem'
                        //col_len = appObj.size.childApps_col_len
                        break;
                    case 'DbPart_mainMenuDb':
                        className = 'mItem'
                        //col_len = appObj.size.mainMenuOrg_col_len
                        break;
                    case 'DbPart_childAppsDb':
                        className = 'cItem'
                        //col_len = appObj.size.childApps_col_len
                        break;
                }
            }
            let startX = className == 'mItem'?appObj.mainMenuDb.left:appObj.childAppsDb.left
            let endX =  className == 'mItem'?appObj.mainMenuDb.left+appObj.mainMenuDb.width : appObj.childAppsDb.left+appObj.childAppsDb.width
            //startX = startX>endX?startX:startX
            
            if(className == 'mItem'){
                endX = endX >= e.pageX ?e.pageX:endX
                mobiex = endX - startX 
            }else{
                //startX = e.pageX<startX?startX:e.pageX
                endX = endX >= e.pageX ?e.pageX:endX
                mobiex = (endX - startX)<0?0: endX - startX

                mobiex = mobiex+appObj.mainMenuDb.width +70

            }
            //+ appObj.mainMenuDb.width + 90
            

            identMobieX = Math.abs(Math.ceil((mobiex - item_w / 2) / item_w));
            identMobieY = Math.abs(Math.ceil((mobiey - item_h / 2) / item_h));

            //console.log(identMobieX,identMobieY)
            identDom.show();
            draw(identDom, className == 'mItem'?identMobieX * item_w - 30:(identMobieX-1) * item_w-30 , identMobieY * item_h -20, 0);
          }
          
          isDrag = true;
        }
    },
    mouseup:function(e){
        //console.log(overPlace)
        
        mouseLastTime = new Date().getTime();
        if( (mouseLastTime - mouseFirstTime) < 200){
            isClick = true;
        }
        if (moveDom == null ) {
            return;
          } else if (isDrag) {
            draw(moveDom, oldCol, oldFew, 0);
            if(overPlace=='') return mouseinit();
            //console.log(overPlace)
            //return

           
            let target = $(e.target)
            let parentClassName = target.parent().attr('class').trim()
            let className = target.hasClass('mItem')?'mItem':'cItem'
            let type = target.parent().data('type')
            let col_len = appObj.size[parentClassName+'_col_len']
            

            if(isSelected){
                switch (startOverPlace){
                    case 'OrgPart_mainMenuOrg':
                        className = 'mItem'
                        col_len = appObj.size.mainMenu_col_len
                        break;
                    case 'OrgPart_childAppsOrg':
                        className = 'cItem'
                        col_len = appObj.size.childApps_col_len
                        break;
                    case 'DbPart_mainMenuDb':
                        className = 'mItem'
                        col_len = appObj.size.mainMenu_col_len
                        break;
                    case 'DbPart_childAppsDb':
                        className = 'cItem'
                        col_len = appObj.size.childApps_col_len
                        break;
                }
            }
            //className=='cItem'?identMobieX=identMobieX-2:identMobieX=identMobieX
            var toposion = identMobieY * col_len + identMobieX;

            if(startOverPlace=='OrgPart_mainMenuOrg'&& className=='mItem' && overPlace.indexOf('DbPart')>-1){

                /**
                 * orgMain -> dbMain
                 * 鼠标拖拽主菜单到打包区域，
                 * 1、Org 菜单 able=false
                 * 2、db菜单 删除 重新加载，重新排序
                 */



                selectedOrgToDb()

                
            }else if(startOverPlace=='OrgPart_childAppsOrg'&& className=='cItem' && overPlace.indexOf('DbPart')>-1){
                /**
                 * orgChild -> dbChild
                 * 1、判断 是否有此主菜单
                 * 
                 */
                
                selectedOrgToDb()
                

            }else if(startOverPlace=='DbPart_mainMenuDb'&& className=='mItem' && overPlace.indexOf('OrgPart')>-1){
                /**
                 * dbMain -> orgMain
                 */

                selectedDbToOrg()

                
            }else if(startOverPlace=='DbPart_childAppsDb'&& className=='cItem' && overPlace.indexOf('OrgPart')>-1){
                /**
                 * dbChild -> orgChild
                 */

                selectedDbToOrg()
                

                
            }else if(startOverPlace=='DbPart_mainMenuDb'&& className=='mItem' && overPlace.indexOf('DbPart')>-1){
                changeSort(toposion,startIndex,appObj.dbMenu,'mainMenu','dbMenu')
            }else if(startOverPlace=='DbPart_childAppsDb'&& className=='cItem' && overPlace.indexOf('DbPart')>-1){

                let dbMenuIndex=0
                let dbParentId = $('.childApps').eq(1).data('id')
                for(let i=0;i<appObj.dbMenu.length;i++){
                    if(appObj.dbMenu[i].busCode==dbParentId){
                        dbMenuIndex=i
                    }
                }
                changeSort(toposion-2,startIndex,appObj.dbMenu[dbMenuIndex].appList,'childApps','dbMenu')
            }
            
          }
          mouseinit();
    },
    mouseleave:function(e){
        if (moveDom == null) {
            return;
          }
    }
})




function selectedOrgToDb(){
    let orgSelectedArr =Object.keys(orgSelectedMap)
    
    if(orgSelectedArr.length>0 && checkItem.hasClass('cur')){
        let hasMenu = {}
        for(let j=0;j<appObj.dbMenu.length;j++){
            for(let i=0;i<orgSelectedArr.length;i++ ){
                let mStr = orgSelectedArr[i].split('_')[0] //
                if(appObj.dbMenu[j].busCode== appObj.Menu[mStr].busCode){
                    hasMenu[mStr] = j
                }
            }
        }

        for(let i=0;i<orgSelectedArr.length;i++ ){
            let strArr = orgSelectedArr[i].split('_')
            let mStr = strArr[0]
            let cStr = strArr[1]
            if(mStr in hasMenu){
                appObj.dbMenu[hasMenu[mStr]].appList.push(JSON.parse(JSON.stringify(appObj.Menu[mStr].appList[cStr])))
            }else{
                let list =JSON.parse(JSON.stringify (appObj.Menu[mStr]))
                list.appList=[JSON.parse(JSON.stringify(appObj.Menu[mStr].appList[cStr]))]
                appObj.dbMenu.push(list)
                hasMenu[mStr] = appObj.dbMenu.length-1
            }

            appObj.Menu[mStr].appList[cStr].able=false
        }
        
        orgSelectedMap={}
        $('.childApps').html('')
    }else{//单选
        let className = checkItem.hasClass('mItem')?'mItem':'cItem'
        let id = checkItem.data('id')
        let indexmap = checkItem.attr('indexmap')
        let indexMapArr = indexmap.split('_')
        let dbMenuIndex = 0
        if(className=='mItem'){
            for(let i=0;i<appObj.dbMenu.length;i++ ){
                if(appObj.dbMenu[i].busCode==id){
                    appObj.dbMenu.splice(i,1)
                    break
                }
            }
            for(let i=0;i<appObj.Menu.length;i++){
                if(appObj.Menu[i].busCode==id){
                    childAble(appObj.Menu[i],true)
                    let newO = JSON.parse(JSON.stringify(appObj.Menu[i]))
                    for(let j=0;j<newO.appList.length;j++){
                      
                        let arr =newO.appList[j].appVersionList.sort()
                        newO.appList[j].appVersionList = [arr[arr.length-1]]
                        
                    }
                    appObj.dbMenu.push(newO)
                    break
                }
            }
            childAble(appObj.Menu[indexmap],false)
            $('.childApps').eq(1).data('id',id)
        }else{
            let parentId = $('.childApps').eq(0).data('id')
            let hasMain = false
            console.log(checkItem)
            let showDbChild = []
            for(let i=0;i<appObj.dbMenu.length;i++){
                if(appObj.dbMenu[i].busCode==parentId){
                    hasMain=true
                    dbMenuIndex=i
                }
            }
            if(appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]].appVersionList.length>1&&$('#selOption_input').val()=='1'){
                let version = checkItem.find('.version').html()
                let appVersionList = appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]].appVersionList
                appVersionList.splice(appVersionList.indexOf(version),1)

                if(hasMain){
                    showDbChild=appObj.dbMenu[dbMenuIndex].appList
                    for(let z=0;z<showDbChild.length;z++){
                        
                        if(indexmap==showDbChild[z].indexMap){
                            //let v = 
                            appVersionList.push(showDbChild[z].appVersionList[0])
                            //delete showDbChild[z]
                            showDbChild.splice(z,1)
                            break
                        }
                    }
                    let o = JSON.parse(JSON.stringify(appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]]))
                    o.appVersionList=[version]
                    showDbChild.push(o)
                    
                }else{
                    let o = JSON.parse(JSON.stringify(appObj.Menu[indexMapArr[0]]))
                    o.appList=[JSON.parse(JSON.stringify(appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]]))]
                    o.appList[0].appVersionList=[version]
                    appObj.dbMenu.push(o)
                }
            }else{
                if(hasMain){
                    showDbChild=appObj.dbMenu[dbMenuIndex].appList
                    showDbChild.push(JSON.parse(JSON.stringify(appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]])))
                    
                }else{
                    let o = JSON.parse(JSON.stringify(appObj.Menu[indexMapArr[0]]))
                    o.appList=[JSON.parse(JSON.stringify(appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]]))]
                    appObj.dbMenu.push(o)
                }
                appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]].able=false
            }
            
            

            $('.childApps').eq(1).data('id',parentId)
        }

        let i = dbMenuIndex==0?appObj.dbMenu.length-1:dbMenuIndex
        addItem(appObj.dbMenu[i].appList,'childApps','dbMenu')
        addItem(appObj.Menu[indexMapArr[0]].appList,'childApps','originMenu')
    }
    
    addItem(appObj.Menu,'mainMenu','originMenu')
    addItem(appObj.dbMenu,'mainMenu','dbMenu')
    
}


function selectedDbToOrg(){
    let dbSelectedArr =Object.keys(dbSelectedMap)
    if(dbSelectedArr.length>0 && checkItem.hasClass('cur')){
        for(let i=0;i<dbSelectedArr.length;i++ ){
            let strArr = dbSelectedArr[i].split('_')
            appObj.Menu[strArr[0]].appList[strArr[1]].able=true
            
        }
    
        
        for(let j=appObj.dbMenu.length-1;j>=0;j--){
            for(let i=appObj.dbMenu[j].appList.length-1 ;i>=0;i-- ){
                
                if(appObj.dbMenu[j].appList[i].indexMap in dbSelectedMap ){
                    appObj.dbMenu[j].appList.splice(i,1)
                    
                }

                if(appObj.dbMenu[j].appList.length==0){
                    appObj.dbMenu.splice(j,1)
                }
            }
        }
    
        console.log(appObj.dbMenu)
    
        dbSelectedMap={}
        $('.childApps').html('')
    }else{
        let className = checkItem.hasClass('mItem')?'mItem':'cItem'
        let id = checkItem.data('id')
        let indexmap = checkItem.attr('indexmap')
        let indexMapArr = indexmap.split('_')
        let dbMenuIndex = 0
        let index = checkItem.attr('item')
        if(className=='mItem'){
            
            delete appObj.dbMenu[index]

            for(let i=0;i<appObj.Menu[indexMapArr[0]].appList.length;i++){
                appObj.Menu[indexMapArr[0]].appList[i].able=true
            }
            addItem(appObj.Menu[indexMapArr[0]].appList,'childApps','originMenu')
            $('.childApps').eq(1).html('')
        }else{
            let dbParentId = $('.childApps').eq(1).data('id')
            for(let j=appObj.dbMenu.length-1;j>=0;j--){
                if(appObj.dbMenu[j].busCode==dbParentId){
                    dbMenuIndex = j
                    delete appObj.dbMenu[j].appList[index]
                    break
                }
            }
            appObj.Menu[indexMapArr[0]].appList[indexMapArr[1]].able=true

            $('.childApps').eq(0).data('id',appObj.Menu[indexMapArr[0]].appId)
            addItem(appObj.dbMenu[dbMenuIndex].appList,'childApps','dbMenu')
            addItem(appObj.Menu[indexMapArr[0]].appList,'childApps','originMenu')
        }
    }
    
    addItem(appObj.Menu,'mainMenu','originMenu')
    addItem(appObj.dbMenu,'mainMenu','dbMenu')
    
}

function newOrAllFun(){
    newOrAll =$('#selOption_input').val();
    let item = $('.childApps').eq(0).data('item')
    addItem(appObj.Menu[item].appList,'childApps','originMenu')
}